package org.honeyweb.gsoc.pages;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.IncludeStylesheet;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.upload.services.UploadedFile;

@IncludeJavaScriptLibrary("context:upload/upload.js")
@IncludeStylesheet("context:upload/upload.css")
public class Upload {

    
	@Component
	private Form run;
	
	@Component
	private Form upload;
	
    @Property
    private UploadedFile file;
    
	@Property
	@InjectComponent
	private Zone urlsZone;
	
	@Property
	@Persist
	private String essai;
    

    public void onSubmitFromUpload()
    {
        File temp = new File("/home/thibaut/" + file.getFileName());
        String inputFileName  = temp.getName();
        doReadWriteTextFile(inputFileName);
        
        file.write(temp);
    }
    
    public void doReadWriteTextFile(String inputFileName) {

        try {
       
            String outputFileName = "temp.txt";

            // Create FileReader Object
            FileReader inputFileReader   = new FileReader(inputFileName);


            // Create Buffered/PrintWriter Objects
            BufferedReader inputStream   = new BufferedReader(inputFileReader);
            PrintStream    outputStream  = new PrintStream(outputFileName);

            String inLine = null;
            ArrayList<String> urls = new ArrayList<String>();
            String test = new String();
            while ((inLine = inputStream.readLine()) != null) {
            	test = test.concat(inLine);
            	test = test.concat("\n");
            }

            outputStream.println(); 
            this.essai = test;
            outputStream.close();
            inputStream.close();
            

        } catch (IOException e) {

            System.out.println("IOException:");
            e.printStackTrace();

        }

    }
    
    public String onSubmitFromRun()
    {
        return "notImplementedYet";
    }

}
